<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>前端咨询展示列表管理</span>
        <div class="extra">
          <el-button type="primary" @click="add">新增咨询</el-button>
        </div>
      </div>
    </template>

    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="咨询编号:">
        <el-input v-model="consult.id" placeholder="请输入咨询编号" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="关键字:">
        <el-input v-model="consult.questio" placeholder="请输入关键字" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="consult.status" placeholder="请选择状态" style="width: 200px">
          <el-option label="已发布" value="已发布"></el-option>
            <el-option label="未发布" value="未发布"></el-option>
            <el-option label="未回复" value="未回复"></el-option>
        </el-select>
        <el-button type="primary" @click="show" style="margin-left: 15px;">搜索</el-button>
        <el-button @click="clear">重置</el-button>
        <el-button type="danger" @click="deleteByIds">批量删除</el-button>
      </el-form-item>

    </el-form>

    <el-table ref="multipleTable" @selection-change="handleSelectionChange" :data="tableData" style=" width: 100%;">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="编号" width="80px"></el-table-column>
      <el-table-column prop="question" label="问题"></el-table-column>
      <el-table-column prop="answer" label="回答"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column label="操作" header-align="center" align="center" width="250">
        <template slot-scope="scope">
          <el-button class="el-icon-edit" type="primary" circle plain @click="updateInfo(scope.row.id)">
          </el-button>
          <el-button class="el-icon-delete" type="danger" circle plain @click="deleteById(scope.row.id)">
          </el-button>

        </template>
      </el-table-column>
      <template #empty>
                <el-empty description="没有数据" />
            </template>
    </el-table>


    <el-pagination style="margin-top: 20px;text-align: right;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :background="background" :current-page="currentPage" :page-sizes="[5, 10]"
      :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="close" width="35%">
        <el-form :model="consult" label-width="50px">
          <el-form-item label="问题" :rules="[{ required: true }]">
            <el-input type="textarea" :rows="4" v-model="consult.question" placeholder="请输入问题简介"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="回答" :rules="[{ required: true }]">
            <el-input type="textarea" :rows="4" v-model="consult.answer" placeholder="请输入回答简介"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="状态"  v-show="!addOrUpdate" :rules="[{ required: true }]">
            <el-select v-model="consult.status" placeholder="请选择状态" style="width: 100%">
              <el-option label="已发布" value="已发布"></el-option>
              <el-option label="未发布" value="未发布"></el-option>
              <el-option label="未回复" value="未回复"></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false;clear()">取消</el-button>
          <el-button type="primary" @click="addForm()">确定</el-button>
        </div>
      </el-dialog>

  </el-card>

  


</template>
<script>


import {addConsult, changeConsult, deleteConsult, getAllConsult} from "@/api/consult";

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 表格数据
      tableData: [],
      addOrUpdate: true,//true为add
      consult: {},
      form: {},
      posts: [],
      multipleSelection: [],
      selectedIds: [],
      consultList: [],
      formLabelWidth: "80px"
    }
  },
  methods: {
    show() {
      getAllConsult(this.consult, this.currentPage, this.pageSize).then((res) => {
        if (res.data.code === 1) {
          this.totalCount = res.data.data.total;
          this.tableData = res.data.data.rows;
          //console.log(this.tableData)
          if (this.tableData.length === 0 && this.currentPage > 1) {
            this.currentPage -= 1;
            this.show();
          }
        }
      }).catch((e) => {
      })
    },

    clear() {
      this.consult = {};
      this.show();
    },
    add() {
      this.consult = {};
      this.addOrUpdate = true;
      this.dialogVisible = true;
      this.title = "咨询录入";
    },
    addForm() {
      if (this.addOrUpdate) {
        addConsult(this.consult).then(res => {
          if (res.data.code === 1) {
            this.$message.success("保存成功")
            this.dialogVisible = false;
            this.show();
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
        });
      } else {
        //更新
        changeConsult(this.consult).then(res => {
          console.log(res)
          if (res.data.code === 1) {
            this.$message.success("更新成功")
            this.dialogVisible = false;
            this.show()
          }
        }).catch(e => {
        });
      }

    },
    close() {
      this.dialogVisible = false;
      this.consult = {}
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    updateInfo(id) {
      this.addOrUpdate = false;
      this.consult = this.tableData.filter(data => data.id == id)[0];
      this.dialogVisible = true;
      this.title = "编辑";
    },
    deleteById(id) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(this.tableData.filter(data => data.id === id)[0]);
      this.deleteByIds()
    },
    deleteByIds() {
      // 弹出确认提示框
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //用户点击确认按钮
        //1. 创建id数组, 从 this.multipleSelection 获取即可
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.selectedIds[i] = this.multipleSelection[i].id;
        }
        //console.log(this.selectedIds);
        deleteConsult(this.selectedIds).then((resp) => {
          if (resp.data.code == "1") {
            //删除成功
            this.$message.success("删除成功");
            this.clear();
          } else {
            this.$message.error(resp.data.msg);
          }
        }).catch(() => {
        });

      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("已取消删除");
      });

    },

    //分页
    handleSizeChange(val) {
      // 重新设置每页显示的条数
      this.pageSize = val;
      this.show();
    },
    handleCurrentChange(val) {
      // 重新设置当前页码
      this.currentPage = val;
      this.show();
    },


  },
  mounted() {
    this.show();
  },
};
</script>


<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


.basic-info-container {
  display: flex; // 使用flex布局
  flex-wrap: wrap; // 允许元素换行
  margin-right: -10px; // 调整外边距以对齐
}

.basic-info-border,
.basic-info-border2 {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  position: relative;
  padding-top: 30px;
  margin-bottom: 20px; // 添加底部外边距
  width: 100%; // 设置宽度为100%，或者您可以设置一个固定的宽度
  box-sizing: border-box; // 确保padding和border包含在宽度内
}

.basic-info-border::before,
.basic-info-border2::before {
  content: "";
  position: absolute;
  top: -15px;
  left: 10px;
  background-color: #409EFF;
  color: white;
  padding: 5px;
  border-radius: 3px;
  font-weight: bold;
}

.basic-info-border::before {
  content: "基本信息";
}

.basic-info-border2::before {
  content: "客户关键人物信息";
}

// 如果需要，可以为两个框设置最大宽度
.max-width-box {
  max-width: 900px; // 或者您希望的任何宽度
}
</style>
<script setup>
</script>
